/**
 * 
 */
package com.xb.cloud.product.merchant.inner.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xb.cloud.common.JsonUtils;
import com.xb.cloud.product.merchant.inner.service.ProductSkuExtPicInnerService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.product.merchant.service.client.input.bo.ProductSkuPicInputBo;
import com.xb.cloud.product.merchant.service.impl.AbstractService;
import com.xb.cloud.product.merchant.service.impl.ProductPublishServiceImpl;
import com.xb.cloud.product.service.business.model.ProductSkuExtPic;
import com.xb.cloud.product.service.business.model.ProductSkuExtPicExample;
import com.xb.cloud.product.service.business.model.mapper.ProductSkuExtPicMapper;

/**
 * sku扩展图片服务类
 * 
 * @author kexue
 *
 */
@Service
public class ProductSkuExtPicInnerServiceImpl extends AbstractService implements ProductSkuExtPicInnerService {

	private static final Logger LOGGER = LoggerFactory.getLogger(ProductPublishServiceImpl.class);

	@Resource
	private ProductSkuExtPicMapper skuExtPicMapper;
	

	/**
	 * 插入sku扩展图片
	 * @param skuPics
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED,value="transactionManager_product")
	public int createSkuExtPics(List<ProductSkuPicInputBo> skuPics, Long productSkuId,
			EndMerchantUserBo endMerchantUserBo) {
		int insertCount=0;
		for (ProductSkuPicInputBo skuPicInput : skuPics) {
			ProductSkuExtPic skuExtPic = new ProductSkuExtPic();
			skuExtPic.setId(ipKeyGenerator.generateKey()); 
			skuExtPic.setPicUrl(skuPicInput.getPicUrl());
			skuExtPic.setPicOrder(skuPicInput.getPicOrder());
			skuExtPic.setSkuId(productSkuId);
			skuExtPic.setIsDefault(skuPicInput.getIsDefault());
			skuExtPic.setCompanyId(endMerchantUserBo.getCompanyId());
			skuExtPic.setCreateTime(timeService.getDate());
			skuExtPic.setCreateUserId(endMerchantUserBo.getId());
			skuExtPic.setCreateUserName(endMerchantUserBo.getLoginName());
			int eleCount = skuExtPicMapper.insertSelective(skuExtPic);
			if(eleCount<=0){
				String message = "保存SkuExtPic失败"+JsonUtils.beanToJson(skuExtPic);
				LOGGER.error(message);
				throw new RuntimeException(message); 
			}else{
				insertCount+=eleCount;
			}
		}
		return insertCount;
	}
	
	/**
	 * 更新sku扩展图片
	 * @param skuPics
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED,value="transactionManager_product")
	public int updateSkuExtPics(List<ProductSkuPicInputBo> skuPics, Long productSkuId,
			EndMerchantUserBo endMerchantUserBo) {
		ProductSkuExtPicExample example=new ProductSkuExtPicExample();
		example.createCriteria()
			.andSkuIdEqualTo(productSkuId)
			.andCompanyIdEqualTo(endMerchantUserBo.getCompanyId());
		skuExtPicMapper.deleteByExample(example);
		
		int updateCount=0;
		for (ProductSkuPicInputBo skuPicInput : skuPics) {
			ProductSkuExtPic skuExtPic = new ProductSkuExtPic();
			skuExtPic.setId(ipKeyGenerator.generateKey()); 
			skuExtPic.setPicUrl(skuPicInput.getPicUrl());
			skuExtPic.setPicOrder(skuPicInput.getPicOrder());
			skuExtPic.setSkuId(productSkuId);
			skuExtPic.setIsDefault(skuPicInput.getIsDefault());
			skuExtPic.setCompanyId(endMerchantUserBo.getCompanyId());
			skuExtPic.setCreateTime(timeService.getDate());
			skuExtPic.setCreateUserId(endMerchantUserBo.getId());
			skuExtPic.setCreateUserName(endMerchantUserBo.getLoginName());
			int eleCount = skuExtPicMapper.insertSelective(skuExtPic);
			if(eleCount<=0){
				String message = "保存SkuExtPic失败"+JsonUtils.beanToJson(skuExtPic);
				LOGGER.error(message);
				throw new RuntimeException(message); 
			}else{
				updateCount+=eleCount;
			}
		}
		return updateCount;
	}
}
